package com.logistics.log.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.logistics.log.entity.domain.Log;
import com.logistics.log.entity.model.LogPager;
import com.logistics.log.entity.model.LogRow;
import com.logistics.log.entity.model.LogSave;
import com.logistics.log.mapper.LogMapper;
import com.logistics.utils.aspect.annotation.Insert;
import com.logistics.utils.aspect.annotation.Pager;
import com.logistics.utils.bean.ResultPager;
import com.logistics.utils.constants.Message;
import com.logistics.utils.exception.MessageException;
import com.logistics.utils.message.Manager;
import com.logistics.utils.tool.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.stream.Collectors;

@Service
public class LogService extends ServiceImpl<LogMapper, Log> {


    /**
     * 查找列表
     * @param param
     * @return
     */
    @Pager
    public ResultPager pager(LogPager param){
        IPage page = new Page(param.getCurrent(), param.getPageSize());
        QueryWrapper<Log> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().like(StringUtils.isNotNull(param.getBusinessName()), Log::getBusinessName, param.getBusinessName());
        queryWrapper.lambda().eq(StringUtils.isNotNull(param.getStatus()), Log::getStatus, param.getStatus());
        queryWrapper.lambda().eq(StringUtils.isNotNull(param.getType()), Log::getType, param.getType());
        queryWrapper.lambda().ge(StringUtils.isNotNull(param.getBeginDate()),
                Log::getCreateTime,
                param.getBeginDate())
                .le(StringUtils.isNotNull(param.getEndDate()),
                        Log::getCreateTime,
                        param.getEndDate());
        IPage<Log> result = this.page(page, queryWrapper.lambda().orderByDesc(Log::getCreateTime));
        return new ResultPager(result.getRecords().stream().map(e -> LogRow.build(e)).collect(Collectors.toList()),
                (int) result.getTotal());
    }

    /**
     * 查找详细
     * @param id
     * @return
     * @throws MessageException
     */
    public LogRow details(String id) throws MessageException {
        if(StringUtils.isNull(id)){
            throw new MessageException(Manager.getMessage(Message.MSG1005.getCode()));
        }
        return LogRow.build(this.getOne(new LambdaQueryWrapper<Log>().eq(Log::getId, id)));
    }

    /**
     * 删除
     * @param ids
     * @throws MessageException
     */
    public void remove(String ids) throws MessageException {
        if(StringUtils.isNull(ids)){
            throw new MessageException(Manager.getMessage(Message.MSG1005.getCode()));
        }
        this.remove(new LambdaQueryWrapper<Log>().in(Log::getId, StringUtils.splitToList(ids, ",")));
    }

    /**
     * 保存
     * @param save
     */
    @Transactional
    @Insert
    public void save(LogSave save){
        this.save(Log.build(save));
    }


}
